<template>
	<view class="my-view view-bg">
		<view :style="{height:navBarHeight + 'px'}">

		</view>
		<view class="user-info">
			<view class="avatar">
				<image class="picture" src="/static/logo.png" mode="aspectFill"></image>
			</view>
			<text class="name">{{ userInfo.IP }}</text>
			<text class="address">{{ formatAddress() }}</text>
		</view>
		<view class="section">
			<view class="list" v-for="(item,index) in list" :key="index" @click="onNavigator(item,index)">
				<view class="icon">
					<uni-icons :type="item.icon" color="#2bb28b"></uni-icons>
				</view>
				<text class="text">{{ item.name }}</text>
				<view class="arrow">
					<text>{{ item.number ? item.number : '' }}</text>
					<uni-icons type="right" color="#ccc"></uni-icons>
				</view>
				<template v-if="item.needBtn">
					<!-- #ifdef MP -->
					<button class="button" open-type="contact">联系客服</button>
					<!-- #endif -->
					<!-- #ifdef H5 -->
					<button class="button" @click="onDial">拨打电话</button>
					<!-- #endif -->
				</template>
			</view>
		</view>
		<view class="section">
			<view class="list" v-for="(item,index) in listT" :key="index">
				<view class="icon">
					<uni-icons :type="item.icon"></uni-icons>
				</view>
				<text class="text">{{ item.name }}</text>
				<view class="arrow">
					<uni-icons type="right" color="#ccc"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		getUserInfo
	} from '@/api/requestInterface.js'
	import {
		useSystemInfo
	} from '@/utils/system.js'
	const {
		navBarHeight,
	} = useSystemInfo() // 系统信息hooks

	const list = ref([{
			icon: 'download-filled',
			needBtn: false,
			name: '我的下载',
			number: 0,
			type: 'download'
		},
		{
			icon: 'star-filled',
			needBtn: false,
			name: '我的评分',
			number: 0,
			type: 'score'
		},
		{
			icon: 'chatboxes-filled',
			needBtn: true,
			name: '联系客服',
			number: 0
		}
	])

	const listT = ref([{
			icon: 'notification-filled',
			name: '订阅更新',
			number: 0
		},
		{
			icon: 'flag-filled',
			name: '常见问题',
			number: 0
		}
	])

	const userInfo = ref({})

	// 功能菜单跳转
	const onNavigator = (item, index) => {

		if (index === 2) return // 只有前两个菜单栏需要
		uni.navigateTo({
			url: `/pages/detail/detail?name=${item.name}&type=${item.type}`
		})
	}

	// 联系客服H5端
	const onDial = () => {
		uni.makePhoneCall({
			phoneNumber: '114'
		})
	}

	// 加载用户数据
	const loadUserInfoData = async () => {
		const {
			errCode,
			data
		} = await getUserInfo()
		if (errCode === 0) {
			userInfo.value = data

			list.value[0].number = userInfo.value.downloadSize
			list.value[1].number = userInfo.value.scoreSize
		}
	}
	onLoad(() => {

		loadUserInfoData()
	})

	// 格式化地址展示
	const formatAddress = () => {

		return userInfo.value.address ?
			`${userInfo.value.address.province} ${userInfo.value.address.city} ${userInfo.value.address.country}` : ''
	}
</script>

<style lang="scss" scoped>
	.my-view {


		.user-info {
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			padding: 50rpx 0 100rpx;

			.avatar {
				width: 160rpx;
				height: 160rpx;
				border-radius: 50%;
				contain: paint;

				.picture {
					width: 100%;
					height: 100%;
				}
			}

			.address {
				color: #aaa;
			}
		}

		.section {
			margin: 0 30rpx;
			margin-bottom: 100rpx;
			border-radius: 10rpx;
			border: 1px solid #eee;
			box-shadow: 0 0 30rpx rgba(0, 0, 0, .05);
			background-color: #fff;

			.list {
				display: flex;
				padding: 20rpx;
				border-bottom: 1px solid #eee;
				position: relative;

				&:last-child {
					border-bottom: none;
				}

				.icon {

					:deep() {

						.uni-icons {
							color: $brand-theme-color !important;
						}
					}
				}

				.text {
					flex: 1;
					padding: 0 10rpx;
				}

				.arrow {
					color: #ccc;
				}

				.button {
					width: 100%;
					height: 100%;
					position: absolute;
					top: 0;
					left: 0;
					font-size: 32rpx;
					border: none;
					opacity: 0;

					&::after {
						border: none;
					}
				}
			}
		}
	}
</style>